package mymath;

import static java.lang.Double.NaN;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import java.util.ArrayList;
import javafx.scene.chart.XYChart;
import static mymath.Functions.MY_FUNCTION.FX2;

public class Functions {
    
    public enum MY_FUNCTION {
        
        FX1, FX2, FX3
    }
    
    private double myFunction(MY_FUNCTION FUNC, double x) {
        double functionValue;
        switch (FUNC) {
            case FX1:
                functionValue = sin(x);
                break;
            case FX2:
                functionValue = cos(x)*sin(x*x);
                break;
            case FX3:
                functionValue = Math.sqrt(x);
                break;            
            default:
                functionValue = NaN;
                break;
        }
        return functionValue;
        
    }
    
    public XYChart.Series getSeriesFx(MY_FUNCTION FUNC) {
        
        ArrayList<Point> points = calcXYPointsForFunction(FUNC, 0, 10, 100);
        XYChart.Series series = getXYChartSeriesForPoints(points);
        
        return series;
    }
    
    private ArrayList<Point> calcXYPointsForFunction(MY_FUNCTION FUNC, double x1, double x2, int numOfSteps) {
        
        ArrayList<Point> points = new ArrayList<>();
        
        double deltaX = (x2 - x1) / numOfSteps;
        double x = x1;
        double y;
        Point p;
        int i = 0;
        boolean isAdded;
        do {
            i++;
            y = myFunction(FUNC, x);
            p = new Point(x, y);
            isAdded = points.add(p);
            x = x + deltaX;
            
        } while (i < numOfSteps);
        System.out.println("x1,x2,i: " + x1 + "/" + x2 + "/" + i);
        
        return points;
    }
    
    private XYChart.Series getXYChartSeriesForPoints(ArrayList<Point> points) {
        //populating the series with data   
        XYChart.Series series = new XYChart.Series();
        //FIXME: should be easier, but I have no idea
        for (Point point : points) {
            series.getData().add(new XYChart.Data(point.getX(), point.getY()));
        }
        return series;
    }
    
}
